package com.ebupt.migu.music.commons.selfdom.demo.impl;

import com.ebupt.migu.globalpopup.entity.ConditionRelation;
import com.ebupt.migu.globalpopup.enums.ConditionEnum;
import com.ebupt.migu.music.commons.selfdom.demo.Account;
import com.ebupt.migu.music.commons.selfdom.demo.impl.identifier.AbstractIdentifierImpl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 账号信息子条件实现类
 *
 * @author wangzhu
 * @since 2021-12-31
 */
public class AccountImpl extends AbstractIdentifierImpl implements Account {

    /**
     * I -- 列表等于（人群）
     * <br>
     * J -- 列表不等于（人群）
     */
    private static final Integer I = 22, J = 23;
    /**
     * uid
     */
    private boolean uid(String data, ConditionRelation conditionRelation,Integer conditionKey) {
        if (conditionRelation.getRelation().equals(I) || conditionRelation.getRelation().equals(J)) {
            return super.getIdentifier(data, conditionRelation.getGroupId(), conditionRelation.getRelation(), conditionKey);
        }
        return super.getIdentifier(data, conditionRelation.getConditionData(), conditionRelation.getRelation(),conditionKey);
    }

    /**
     * 用户手机号
     */
    private boolean phoneNumber(String data, ConditionRelation conditionRelation,Integer conditionKey) {
        if (conditionRelation.getRelation().equals(I) || conditionRelation.getRelation().equals(J)) {
            return super.getIdentifier(data, conditionRelation.getGroupId(), conditionRelation.getRelation(), conditionKey);
        }
        return super.getIdentifier(data, conditionRelation.getConditionData(), conditionRelation.getRelation(),conditionKey);
    }

    /**
     * 更具不同的子条件判断返回判断结果（子条件第一版只支持or）
     */
    @Override
    public boolean getAccount(Map data, List<ConditionRelation> conditionRelations) {
        LinkedHashMap<Boolean, Integer> booleanIntegerLinkedHashMap = new LinkedHashMap<>();
        // 排序
        super.sort(conditionRelations);
        for (ConditionRelation conditionRelation : conditionRelations) {
            boolean account = true;
            Object o = data.get(ConditionEnum.getKeyValue(conditionRelation.getPropertyCode(), conditionRelation.getConditionKey()));
             if (o==null||o.equals("")) {
                 account=false;
                 booleanIntegerLinkedHashMap.put(account,conditionRelation.getCConnector());
                 break;
             }
            String value = o.toString();
            switch (conditionRelation.getConditionKey()) {
                case 1:
                    account = this.uid(value, conditionRelation,conditionRelation.getConditionKey());
                    break;
                case 2:
                    account = this.phoneNumber(value, conditionRelation,conditionRelation.getConditionKey());
                    break;
                default:
                    break;
            }
            booleanIntegerLinkedHashMap.put(account,conditionRelation.getCConnector());
        }
        return super.getRelation(booleanIntegerLinkedHashMap);
    }
}
